package com.situ.stl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stl.dao.CourseMapper;
import com.situ.stl.dao.ReserveMapper;
import com.situ.stl.entity.Course;
import com.situ.stl.entity.Reserve;
import com.situ.stl.service.ReserveService;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/** 
 * Service层 - 预约
 */
@Service 
public class ReserveServiceImpl implements ReserveService{

    /** 
     */
    @Autowired 
    private ReserveMapper reserveMapper;
    @Autowired
    private CourseMapper courseMapper;

    /** 
     * add
     */
    @Override 
    public int add( Reserve reserve) throws Exception{
        // 先检查是否已经有预约记录
        Reserve sReserve = reserveMapper.selectByMemberAndCourse(reserve);
        if ( ! ObjectUtils.isEmpty(sReserve)) {
            throw new Exception("已存在预约信息，不可再次预约！");
        }
        // 核对课程信息
        Course course = courseMapper.selectByPrimaryKey(reserve.getCourseId());
        if (ObjectUtils.isEmpty(course)) {
            throw new Exception("课程不存在，无法预约！");
        }
        // 核对开课时间
        Date now = new Date();
        System.out.println(now.getTime() - course.getTime().getTime());
        if (now.getTime() - course.getTime().getTime() > 60*60*1000) {
            throw new Exception("必须在开课一小时前预约！");
        }


        int res = reserveMapper.insert(reserve);
        return res;
    }

    /** 
     * remove
     */
    @Override 
    public int remove( int id) {
        int res = reserveMapper.delete(id);
        return res;
    }

    /** 
     * edit
     */
    @Override 
    public int edit( Reserve reserve) {
        int res = reserveMapper.update(reserve);
        return res;
    }

    @Override
    public int batchReserve(List<Reserve> reserves) throws Exception {
        if (reserves.isEmpty()){
            throw new Exception("请至少选择一条预约！");
        }
        Integer status = reserves.get(0).getStatus();
        return reserveMapper.batchUpdate(reserves, status);
    }

    /** 
     * getById
     */
    @Override 
    public Reserve getById( int id) {
        return reserveMapper.selectByPrimaryKey(id);
    }

    /** 
     * get All
     */
    @Override 
    public List<Reserve> getAll() {
        return reserveMapper.selectAll();
    }

    /** 
     * search
     */
    @Override 
    public List<Reserve> search( Reserve reserve) {
        return reserveMapper.select(reserve);
    }

    /** 
     * searchByPage
     */
    @Override 
    public PageInfo searchByPage( Integer page, Integer limit, Reserve reserve) {
        if (page == null) page = 1;
        if (limit == null) limit = 10;
        
        PageHelper.startPage(page, limit);
        List list = reserveMapper.select(reserve);
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

}